function saveConfig(){
    var o = document.config;
    var p =/^[1-9]d*$/;
    if(o.tryTimes.value==""){
        o.tryTimes.focus();
        alert("登陆限制不能空！");
        return;
    }else if(!p.test(o.tryTimes.value)){
        o.tryTimes.focus();
        alert("登陆限制次数只能是正整数！");
        return;
    }

    var ip = document.getElementsByName("ip");
    var ipvalue = "false";
    for(var i=0;i<ip.length;i++) {
        if(ip.item(i).checked){
            ipvalue=ip.item(i).value;
            break;
        }
    }

    var type = document.getElementsByName("type");
    var typevalue = 0;
    for(i=0;i<type.length;i++) {
        if(type.item(i).checked){
            typevalue=type.item(i).value;
            break;
        }
    }

    var length = $("ip_list_black").length;
    var b="",w="";
    for(i=0; i<length; i++){
        if(i<length-1){
            b+=$("ip_list_black").options[i].value+",";
        }
        else{
            b+=$("ip_list_black").options[i].value;
        }
    }
    length = $("ip_list_white").length;
    for(i=0; i<length; i++){
        if(i<length-1){
            w+=$("ip_list_white").options[i].value+",";
        }
        else{
            w+=$("ip_list_white").options[i].value;
        }
    }

    if(typevalue==1){
        if(w==""){
            alert("您必须填写白名单IP，否则后台将无法访问！");
            return;
        }
    }

    $('save').disabled='disabled';
    manage.saveConfig(o.tryTimes.value,ipvalue,typevalue,b,w,function(data){
        if(data==1){
            alert("保存成功！");
        }else{
            alert("未知原因，保存失败！");
            $('save').removeAttribute('disabled');
        }
    }
    );
}

function appendIP(type, ip){
    if(ip==""){
        alert("IP不能空");
    }else{
        var pattern=/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
        var index = ip.indexOf("-");
        if(index==-1){
            if(!pattern.test(ip)){
                alert("IP地址非法！格式为xxx.xxx.xxx.xxx。");
                return;
            }
        }else{
            var ip1 = ip.substring(0,index);
            var ip2 = ip.substring(index+1);
            if(!pattern.test(ip1)||!pattern.test(ip2)){
                alert("IP地址非法！格式为xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx。");
                return;
            }else{
                var p1 = ip1.split(".");
                var p2 = ip2.split(".");

                if(p1.length!=4){
                    return;
                }else{
                    var a0 = parseInt(p1[0]);
                    var a1 = parseInt(p1[1]);
                    var a2 = parseInt(p1[2]);
                    var a3 = parseInt(p1[3]);
                    
                    var b0 = parseInt(p2[0]);
                    var b1 = parseInt(p2[1]);
                    var b2 = parseInt(p2[2]);
                    var b3 = parseInt(p2[3]);
                    
                    if((a0>b0)||
                        ((a0==b0)&&(a1>b1))||
                        ((a0==b0)&&(a1==b1)&&(a2>b2))||
                        ((a0==b0)&&(a1==b1)&&(a2==b2)&&(a3>=b3))){
                        alert("IP地址非法！格式为xxx.xxx.xxx.xxx-xxx.xxx.xxx.xxx。\r\n后面的IP必须大于前面的IP！");
                        return;
                    }
                }
            }
        }

        var ipList;
        if(type==0){
            ipList = $("ip_list_black");
        }else{
            ipList = $("ip_list_white");
        }

        var length = ipList.length;
        for(i=0; i<length;i++){
            if(ipList.options[i].value==ip){
                alert("该IP已经存在！");
                return;
            }
        }
        ipList.options[length] = new Option(ip,ip);
    }
}

function removeIP(type, ip){
    if(ip==""){
        alert("请选择您要删除的IP");
    }else{
        var ipList;
        if(type==0){
            ipList = $("ip_list_black");
        }else{
            ipList = $("ip_list_white");
        }
        ipList.remove(ipList.selectedIndex);
    }
}